﻿using Hotel.DAL;
using Hotel.Model.Member;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MD5Hash;

namespace Hotel.BLL
{
    public class MemberBLL
    {
        MemberDAL dal;
        public MemberBLL(MemberDAL dal)
        {
            this.dal = dal;
        }

        /// <summary>
        /// 完成会员注册
        /// </summary>
        /// <param name="info">注册的实体</param>
        /// <returns>返回受影响的行数</returns>
        public int PostMemberRegiter(T_Member info)
        {
            try
            {
                //1.密码加密
                info.M_Pwd = info.M_Pwd.GetMD5();
                //2.手机号不能重复
                int res = dal.SearchPhone(info.M_Phone);
                if(res > 0)
                {   //手机号存在
                    return -1;
                }
                else
                {
                    int result = dal.PostMemberRegiter(info);
                    if(result > 0)
                    {
                        //表示注册成功
                        return 1;
                    }
                    else
                    {
                        //表示注册失败
                        return -2;
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 会员登录
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <param name="pwd">密码</param>
        /// <returns>返回符合条件的数据</returns>
        public T_Member GetMemeberLogin(string phone, string pwd)
        {
            try
            {
                T_Member info = dal.GetMemeberLogin(phone, pwd);
                return info;
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 获取会员列表
        /// </summary>
        /// <param name="phone">根据手机号查询</param>
        /// <returns>返回符合条件的数据</returns>
        public List<T_Member> GetMembers(string? phone = "")
        {
            try
            {
                List<T_Member> list = dal.GetMembers(phone);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public int GetDelMemberItem(int mid)
        {
            try
            {
               int res = dal.GetDelMemberItem(mid);
                if(res > 0)
                { 
                    //1表示成功
                    return 1; 
                }
                else
                {
                    //表示失败
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据id修改状态
        /// </summary>
        /// <param name="mid">id</param>
        /// <param name="state">转台</param>
        /// <returns></returns>
        public int GetUpdateMemberItem(int mid, int state)
        {
            try
            {
                var res = dal.GetUpdateMemberItem(mid, state);
                if(res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="infos"></param>
        /// <returns></returns>
        public int PostAddMemberList(List<T_Member> infos)
        {
            try
            {
                int res =  dal.PostAddMemberList(infos);

                if(res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据会员的ID获取会员信息
        /// </summary>
        /// <param name="Mid">会员id</param>
        /// <returns>返回符合条件的数据</returns>
        public T_Member GetMember(int Mid)
        {

            try
            {
                T_Member info = dal.GetMember(Mid);
                return info;
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 对会员进行修改
        /// </summary>
        /// <param name="Mid">会员id</param>
        /// <returns>返回影响的行数</returns>
        public int PostUpdateMember(T_Member info)
        {
            try
            {
                int res = dal.PostUpdateMember(info);
                if(res > 0)
                {
                    //表示修改成功
                    return 1;
                }
                else
                {
                    //表示修改失败
                    return -2;
                }

            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加会员等级
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int PostAddMemberLevel(T_Member_Level info)
        {
            try
            {
                int r = dal.SearchLevelName(info.LevelName);
                if(r > 0)
                {
                    return -1;
                }
                else
                {
                    int res = dal.PostAddMemberLevel(info);
                    if (res > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        return -2;
                    }
                }


                
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 等级列表
        /// </summary>
        /// <param name="totalPage">总页数</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="LevelName">等级名称</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        public List<T_Member_Level> GetMemberLevelList(out int totalPage, out int totalCount, string? LevelName = "", int pageIndex = 1, int pageSize = 2)
        {
            try
            {
                //所有的等级列表
                List<T_Member_Level> list = dal.GetMemberLevelList(out totalPage,out totalCount,LevelName,pageIndex,pageSize);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加会员权益
        /// </summary>
        /// <param name="info">会员权益</param>
        /// <returns>返回影响的行数</returns>
        public int PostAddMemberRight(T_Member_Right info)
        {
            try
            {
               //1.判断一下权益名称是重复
               int r = dal.SeachRightName(info.RightName);
                if(r > 0)
                {
                    //权益名称是否存在
                    return -1;
                }
                else
                {
                    int res = dal.PostAddMemberRight(info);
                    if(res > 0)
                    {
                        return 1;
                    }
                    else
                    {
                        return -2;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取权益列表
        /// </summary>
        /// <returns></returns>
        public List<T_Member_Right> GetMemberRights()
        {
            try
            {
                List<T_Member_Right> list = dal.GetMemberRights();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 增加等级权益
        /// </summary>
        /// <param name="rightArr">权益id</param>
        /// <param name="levelId">等级id</param>
        /// <returns></returns>
        public int PostAddLevelRight(string rightArr, int levelId)
        {
            try
            {
                
                int res = dal.PostAddLevelRight(rightArr, levelId);
                if(res > 0)
                {
                    return 1;
                }
                else
                {
                    return -2;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据等级id去查他的权益
        /// </summary>
        /// <param name="levevlId">等级id</param>
        /// <returns>返回符合条件的数据</returns>

        public List<T_Level_Right> GetLevelRightList(int levevlId)
        {
            try
            {
                List<T_Level_Right> list = dal.GetLevelRightList(levevlId);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
